﻿namespace PageTypeBuilder.SortOrderChecker
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    class Program
    {

        public enum Action
        {
            CheckSortOrders = 0,
            CheckSortOrdersPostBuild = 1,
            FixSortOrders = 2,
            GenerateSql = 3
        }

        [STAThreadAttribute]
        static void Main(string[] arguments)
        {

            //arguments = new[] { "-t:0", @"-b:c:\episerver\sites\pagetypebuildertest\bin" };
            //arguments = new[] { "-t:1", @"-b:c:\episerver\sites\pagetypebuildertest\bin" };
            //arguments = new[] { "-t:2", @"-b:c:\episerver\sites\pagetypebuildertest\bin", @"-p:c:\episerver\sites\pagetypebuildertest\" };
            //arguments = new[] { "-t:3", @"-b:c:\episerver\sites\pagetypebuildertest\bin", @"-o:c:\episerver\sites\pagetypebuildertest\test.sql" };

            Console.WriteLine();

            if (arguments.Length == 0 || arguments.Length == 1 && (arguments[0] == "?" || arguments[0] == "/?"))
            {
                Console.WriteLine(@"Example usage: -t:0 -b:c:\episerver\sites\pagetypebuildertest\bin");
                Console.WriteLine();
                Console.WriteLine("   -t:0 = Check sort orders command line");
                Console.WriteLine("      1 = Check sort orders post build");
                Console.WriteLine("      2 = Fix sort orders");
                Console.WriteLine("      3 = Generate sql to reset sort orders");
                Console.WriteLine("   -b: Bin folder path");
                Console.WriteLine("   -p: Project folder path");
                Console.WriteLine("   -o: Output file path");
                Console.ReadLine();
                Environment.Exit(0);
            }

            Action action = Action.CheckSortOrders;
            DirectoryInfo binFolder = null;
            DirectoryInfo projectFolder = null;
            string outputFilePath = string.Empty;
            string errorMessage = ProcessCommandLineArguments(arguments, ref action, ref binFolder, ref projectFolder, ref outputFilePath);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                Console.WriteLine(errorMessage);
                Console.ReadLine();
                Environment.Exit(1);
            }

            if (action == Action.CheckSortOrders || action == Action.CheckSortOrdersPostBuild)
            {
                SortOrderChecker sortOrderChecker = new SortOrderChecker();
                bool returnValue = sortOrderChecker.CheckSortOrders(SortOrderChecker.RunType.CommandLine, binFolder.FullName, false, null);

                if (action == Action.CheckSortOrdersPostBuild && !returnValue)
                    Environment.Exit(-1);
            }
            else if (action == Action.FixSortOrders)
            {
                SortOrderFixer sortOrderFixer = new SortOrderFixer();
                sortOrderFixer.Fix(binFolder.FullName, projectFolder.FullName);
            }
            else if (action == Action.GenerateSql)
            {
                SortOrderFixer sortOrderFixer = new SortOrderFixer();
                sortOrderFixer.GenerateSql(binFolder.FullName, outputFilePath);
            }

            Console.ReadLine();
        }

        private static string ProcessCommandLineArguments(IEnumerable<string> arguments, ref Action action, 
            ref DirectoryInfo binFolder, ref DirectoryInfo projectFolder, ref string outputFilePath)
        {
            string errorMessage = string.Empty;

            foreach (string argument in arguments)
            {
                if (argument.StartsWith("-t:"))
                {
                    string value = GetValue(argument.Substring(3));
                    Action parsedAction;

                    if (Enum.TryParse(value, out parsedAction))
                        action = parsedAction;
                }
                else if (argument.StartsWith("-b:"))
                {
                    string folderPath = GetValue(argument.Substring(3));

                    if (!folderPath.EndsWith(@"\"))
                        folderPath += @"\";

                    binFolder = new DirectoryInfo(folderPath);
                }
                else if (argument.StartsWith("-p:"))
                {
                    string folderPath = GetValue(argument.Substring(3));

                    if (!folderPath.EndsWith(@"\"))
                        folderPath += @"\";

                    projectFolder = new DirectoryInfo(folderPath);
                }
                else if (argument.StartsWith("-o:"))
                {
                    outputFilePath = GetValue(argument.Substring(3));
                }
            }

            if ((action == Action.CheckSortOrders || action == Action.CheckSortOrdersPostBuild) && (binFolder == null || !binFolder.Exists))
            {
                errorMessage = "You must specify a bin folder that exists.";
            }
            else if ((action == Action.FixSortOrders) && (binFolder == null || !binFolder.Exists || projectFolder == null || !projectFolder.Exists))
            {
                errorMessage = "You must specify an existing bin and project folder.";
            }
            else if ((action == Action.GenerateSql) && (binFolder == null || !binFolder.Exists || string.IsNullOrEmpty(outputFilePath)))
            {
                errorMessage = "You must specify a bin folder that exists and an output file path.";
            }

            return errorMessage;
        }

        private static string GetValue(string value)
        {
            return value.Trim('"', ' ');
        }
    }
}
